﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.DAL;

public partial class FilesPage_FileManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((ScriptManager)Master.Master.FindControl("ScriptManager1")).RegisterPostBackControl(ImageButtonSave);//给按钮注册脚本
        FilesPage_FileIndex.SonPage = "FileManage";//指明页面正在使用母版页
        if (!IsPostBack)
        {
            User user = (User)Session["userManage"];
            LabelUserName.Text = user.UserName;
            ShowRadionBtn();
            JudgePage();
        }
    }

    #region Radionbutton绑定数据
    /// <summary>
    /// RadionBtn绑定数据
    /// </summary>
    private void ShowRadionBtn()
    {
        List<DocumentType> dtlits = FileOperationBLL.SelectAllDocumentType();
        foreach (var item in dtlits)
        {
            //绑定图片信息
            ListItem listitem = new ListItem();
            listitem.Text = "<img src='" + item.DocTypeImage.ToString() + "'/>";
            listitem.Value = item.DocTypeNo.ToString();
            RadioButtonListImage.Items.Add(listitem);
        }
        RadioButtonListImage.SelectedIndex = 0;
    } 
    #endregion

    #region 界面的数据绑定
    /// <summary>
    /// 界面的数据绑定
    /// </summary>
    private void JudgePage()
    {
        FileUpLoad.Visible = false;//控件隐藏
        if (Request.QueryString["id"] != null)
        {
            //界面的数据获取
            int fileNo = Convert.ToInt32(Request.QueryString["id"].ToString());
            List<v_SelectAllDocumentManage> files = FileOperationBLL.SelectOneFile(fileNo);
            TextBoxFileName.Text = files[0].DocId.ToString();
            TextBoxPosition.Text = files[0].DocPath.ToString();
            LabelDateTime.Text = files[0].DocCreatDate.ToString();
            Session["trueFileName"] = files[0].DocPath.ToString().Substring(files[0].DocPath.ToString().LastIndexOf("\\"));//用session保存真正的文件名称
            if (files[0].DocMemo != null)
            {
                TextBoxNote.Text = files[0].DocMemo.ToString();
            }
            string docType = files[0].DocTypeNo.ToString();
            User user = (User)Session["userManage"];
            LabelUserName.Text = user.UserId.ToString();
            foreach (ListItem item in RadioButtonListImage.Items)
            {
                if (item.Value.ToLower().Trim().Equals(docType.Trim().ToLower()))
                {
                    item.Selected = true;
                }
            }
            if (docType=="1")//如果是文件夹，就显示文件夹里面的文件
            {
                FileUpLoad.Visible = true;
                GridViewFiles.DataSource = FileOperationBLL.SelectAllFile(Convert.ToInt32(docType));
                GridViewFiles.DataBind();
            }
        }
        else
        {
            FileUpLoad.Visible = true;
        }
    } 
    #endregion

    protected void AsyncFileUploadFiles_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        //AsyncFileUploadFiles.SaveAs(Server.MapPath("~/UserFile") + AsyncFileUploadFiles.FileName);
    }
    protected void deleted_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btn = sender as ImageButton;
        int rowIndex = ((GridViewRow)btn.NamingContainer).RowIndex;
        string fileNo = (GridViewFiles.Rows[rowIndex].FindControl("DocId") as Label).Text;
        FileOperationBLL.DeletToRecycleByDocId(Convert.ToInt32(fileNo));
    }
    protected void ImageButtonSave_Click(object sender, ImageClickEventArgs e)
    {
        if ( AsyncFileUploadFiles.HasFile)//上传文件
        {
            string fileType = System.IO.Path.GetExtension(AsyncFileUploadFiles.FileName);
            string filename = TextBoxFileName.Text + "_" + DateTime.Now.Millisecond.ToString() + fileType;
            string filePath = Server.MapPath("~/UserFiles/") + filename;
            Document docment = new Document();
            User userNo = (User)Session["userManage"];
            docment.DocId = TextBoxFileName.Text;
            docment.DocMemo = TextBoxNote.Text;
            docment.DocPath = TextBoxPosition.Text + "\\" + filename;
            docment.DocCreatDate = DateTime.Now;
            docment.IsDelete = false;
            if (FileOperationBLL.AddFile(docment, userNo.UserNo, Convert.ToInt32(RadioButtonListImage.SelectedValue), TextBoxPosition.Text.ToLower().Trim()))
            {
                AsyncFileUploadFiles.SaveAs(filePath);
                Response.Write("<script>alert('上传成功');window.location.href='ShowFiles.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('上传失败了');window.location.href='ShowFiles.aspx'</script>");
            }
        }
        else if (Request.QueryString["id"] != null)//修改文件
        {
            Document docment = new Document();
            docment.DocNo = Convert.ToInt32(Request.QueryString["id"].ToString());
            docment.DocId = TextBoxFileName.Text;
            docment.DocMemo = TextBoxNote.Text;
            docment.DocPath = TextBoxPosition.Text;
            string doctype = RadioButtonListImage.SelectedValue;
            if (FileOperationBLL.UpdateFile(docment, Convert.ToInt32(doctype), Session["trueFileName"].ToString()))
            {
                Response.Write("<script>alert('修改成功了');window.location.href='ShowFiles.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败了');window.location.href='ShowFiles.aspx'</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('上传或修改文件出现了错误。请重新操作');window.location.href='ShowFiles.aspx'</script>");
        }
    }
    protected void ImageButtonExit_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/FilesPage/ShowFiles.aspx");
    }
}